<?php
// TODO: dodawanie, edytowanie, usuwanie newsow
session_start();
class Controller_User_News extends Controller_User_Abstract
{	
	public function __construct(Controller_Front $frontController)
	{
		parent::__construct('user_news', $frontController);
	}
	
	public function indexAction()
	{
		$this->view()->display('user_news.tpl');
	}
	
	public function newAction()
	{
		$buttonName = 'Dodaj';
		$news = new News();
		
		$this->view()->assign('news', $news);
		$this->view()->assign('vortals', VortalsPeer::doSelect(new Criteria()));
		$this->view()->assign('editButton', $buttonName);
		$_SESSION['enable']=true;
		$this->view()->display('user_news_edit.tpl');
	}
	
	public function checkAction()
	{		
		$news;
		
		if (!empty($_POST['id_news'])) {
			$id = $_POST['id_news'];
			
			$news = newsPeer::retrieveByPK($id);
		} else {
			$news = new News();
		}
		
		if($_SESSION["enable"]==true) {
			$news->setTitle($_POST['title']);
			$news->setUrlOriginal($_POST['url']);
			$news->setIntro($_POST['intro']);
			$news->setStory($_POST['story']);
			$news->setUrlImage($_POST['url_image']);
			//var_dump($category->validate('name'));
			$news->setIdUser(Manager_User::getUser()->getIdUser());
			$news->setIdVortal($_POST['id_vortal']);
			$news->setIdRequestStatus('1');
			$news->setCategories(Manager_Category::getDefault());
			$news->save();
			$this->ok('News saved.');
				
		
		
		$tags = Manager_Tag::add(Manager_Tag::fromString($_POST['tags']));
		Manager_News::addTags($news, $tags);
		
		$news->setCategories(Manager_News::getBestCategory($news));
		$news->save();
		//var_dump(Manager_News::getBestCategory($news));
		$this->ok('News was added and is awaiting for acceptance.');
		$_SESSION['enable']=false;	
		} else {
			$this->fail('Re-saving it is not possible.');
		}
	}	
	
	
}